#include <iostream>
#include <SDL.h>
#include <unistd.h>


using namespace std;
bool init();
void kill();
bool load();

SDL_Window* window;
SDL_Surface* winSurface;
SDL_Surface* image1;
SDL_Surface* image2;




int main(int argc,const char* argv[] ) {

    if ( !init() ) return 1;
    if ( !load() ) return 1;

    SDL_BlitSurface( image1, NULL, winSurface, NULL);

    SDL_Rect dest;
    dest.x = 160;
    dest.y = 120;
    dest.w = 320;
    dest.h = 240;

    SDL_BlitScaled( image2, NULL, winSurface, &dest);

    SDL_UpdateWindowSurface( window );

    pause();
    kill();

    return 0;
}

bool init() {
    if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) {
        cout << "Error initializing SDL: " << SDL_GetError() << endl;
        pause();
        return false;
    }

    window = SDL_CreateWindow("example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN );
    if ( !window ) {
        cout << "Error Creating window" << SDL_GetError() << endl;
        pause();
        return false;
    }

    winSurface = SDL_GetWindowSurface( window );
    if ( !winSurface ) {
        cout << "Error getting surface: " << SDL_GetError() << endl;
        pause();
        return false;
    }
    return true;
}


void kill() {
    SDL_FreeSurface( image1 );
    SDL_FreeSurface( image2 );

    SDL_DestroyWindow( window );
    SDL_Quit();
}

bool load() {

    SDL_Surface *tmp1,*tmp2;

    tmp1 = SDL_LoadBMP("test1.bmp");
    tmp2 = SDL_LoadBMP("test2.bmp");

    if ( !tmp1 || !tmp2 ) {
        cout << "Error loading image: " << SDL_GetError() << endl;
        pause();
        return false;
    }
    image1 = SDL_ConvertSurface( tmp1, winSurface->format,0);
    image2 = SDL_ConvertSurface( tmp2, winSurface->format,0);
  
    if ( !image1 || !image2 ) {
        cout << "Error convering surface: " << endl;
        pause();
        return false;
    }
    return true;
}